------- Purpose:  Kiem tra xem mot  mat hang da duoc su dung hay chua.
-------------------------------------------------------------------
---purpose: Kiem tra asoft-OP, Asoft-T
DROP PROCEDURE [dbo].[pr0078]
GO
CREATE  PROCEDURE [dbo].[pr0078] 	@InventoryID as varchar(20)			
				
 AS

Declare @Status as tinyint,
	@IsAsoftM as tinyint,
	@IsAsoftOP as tinyint	
	
Select @Status =0, @IsAsoftM = IsAsoftM, @IsAsoftOP = IsAsoftOP From tb0000
------- Bang so du
If Exists (Select  top 1  1  From  tb0110 Where InventoryID = @InventoryID)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End
----- Bang but toan
If Exists (Select  top 1  1  From  tb0191  Where InventoryID = @InventoryID)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End

----- Bang phieu nhap xuat
If Exists (Select  top 1   1  From  tb0106 	Where InventoryID = @InventoryID
	Having Sum(BeginQuantity)<>0 or Sum(EndQuantity)<>0 or Sum(DebitQuantity)<>0 or Sum(CreditQuantity)<>0
	)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End

---Bang don hang ban
If @IsAsoftOP = 1
If Exists (Select  top 1   1  From  tb3524 	Where InventoryID = @InventoryID)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End

---Bang don hang mua
If @IsAsoftOP = 1
If Exists (Select  top 1   1  From  tb3538 	Where InventoryID = @InventoryID)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End

---Bang chao gia
If @IsAsoftOP = 1
If Exists (Select  top 1   1  From  tb3532 	Where InventoryID = @InventoryID)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End

---Bang  ke hoach san xuat
If @IsAsoftM = 1
If Exists (Select  top 1   1  From  tb3039 	Where InventoryID = @InventoryID)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End

----Bang tien do san xuat
If @IsAsoftM = 1
If Exists (Select  top 1   1  From  tb3042 	Where InventoryID = @InventoryID)
	Begin
		 Set @Status =1
		GOTO RETURN_VALUES
	End

---- Tra ra gia tri
RETURN_VALUES:
Select @Status as Status


GO